

********

* load data
clear all
use "data/both.dta", clear

* prep data
gen sample=1
collapse (sum) sample, by(roll1 treat)
reshape wide sample, i(roll1) j(treat)
keep roll1 sample21 sample22 

* convert all to %s. 
forvalues i=21/22 { 
sum sample`i'
replace sample`i'=100*sample`i'/r(sum)
}


* set up a results dataset
frame create results
frame change results
set obs 0
gen sample = .
gen aic_H = . 
gen rmse_H = . 
gen aic_JD = . 
gen rmse_JD = . 
gen aic_DD = . 
gen bic_DD = . 
gen rmse_DD = . 
gen aic_KS = . 
gen bic_KS = . 
gen rmse_KS = . 
frame change default

* loop over models and samples. 
gen outcome=roll1 
gen reports=.
gen theta_hat=.
gen dd_hat=.
gen h_hat=100/6
gen jd_hat=100*((1+2*roll1)/36)


forvalues i=21/22 {
	replace reports=sample`i'/100

	** run the do files to fit dd and ks, and save their predictions
	do "code/fit dd 1 roll.do"
	gen theta_hat`i' 	= theta_hat
	gen dd_hat`i'		= dd_hat*100
	do "code/fit ks 1 roll.do"
	rename predictions_ks ks_hat`i'	
	
	** save model fit stats
	
	constraint 1 h_hat = 1
	cnsreg sample`i' h_hat , constraint(1) nocons
	local rmse =  e(rmse) * sqrt(e(df_r) / e(N)) // to get uncorrected RMSE - stata corrects for DF but economists don't it seems. 
	estat ic, n(6) df(0)
	local loglike5 = r(S)[1,5]
	local loglike = r(S)[1,6]
	frame change results
	set obs `=_N+1'
	replace sample = `i' if _n==_N
	replace rmse_H = `rmse' if _n==_N
	replace aic_H = `loglike5' if _n==_N
	frame change default

	constraint 2 jd_hat = 1
	cnsreg sample`i' jd_hat , constraint(2) nocons
	local rmse =  e(rmse) * sqrt(e(df_r) / e(N))
	estat ic, n(6) df(0)
	local loglike5 = r(S)[1,5]
	local loglike = r(S)[1,6]
	frame change results
	replace rmse_JD = `rmse' if _n==_N
	replace aic_JD = `loglike5' if _n==_N
	frame change default
	
	constraint 3 dd_hat`i' = 1
	cnsreg sample`i' dd_hat`i', constraint(3) nocons
	local rmse =  e(rmse) * sqrt(e(df_r) / e(N))
	estat ic, n(6) df(1)
	local loglike5 = r(S)[1,5]
	local loglike = r(S)[1,6]
	frame change results
	replace rmse_DD = `rmse' if _n==_N
	replace aic_DD = `loglike5' if _n==_N
	replace bic_DD = `loglike' if _n==_N
	frame change default

	constraint 4 ks_hat`i' = 1
	cnsreg sample`i' ks_hat`i', constraint(4) nocons
	local rmse =  e(rmse) * sqrt(e(df_r) / e(N))
	estat ic, n(6) df(2)
	local loglike5 = r(S)[1,5]
	local loglike = r(S)[1,6]
	frame change results
	replace rmse_KS = `rmse' if _n==_N
	replace aic_KS = `loglike5' if _n==_N
	replace bic_KS = `loglike' if _n==_N
	frame change default
	}


frame change results

gen BF_h_jd=exp(.5*(aic_H-aic_JD))
gen BF_dd_jd=exp(.5*(bic_DD-aic_JD))
gen BF_ks_jd=exp(.5*(bic_KS-aic_JD))
list sample bic* BF*


order sample rmse* aic* bic*  
format rmse*  %9.3f
format aic* bic*  %9.2f

list sample rmse*  aic* bic*

list sample aic* 
list sample bic* 
list sample rmse* 

list sample rmse*  aic* bic*


preserve
keep sample rmse* aic* bic*
texsave using "output/appendixtable4.tex", varlabels hlines(-2) nofix replace frag









